﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.Framework.Authentication;
using System.Data;
using Wicresoft.JDPostBoard.Entity;
using Wicresoft.Framework.Organization;
using Wicresoft.Common;

public partial class Gift_ChargeAdmin : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("UserID");

            dt.Columns.Add("UserName");

            dt.Columns.Add("UserDisplayName");

            dt.Columns.Add("DepartmentIDs");

            dt.Columns.Add("DepartmentNames");
            dt.Columns.Add("DepartmentDisplayNames");

            DataTable alldt = DataAccess.Select(typeof(Giftcharge), "1=1");
            DataTable allUser = DataAccess.ExecuteSql("JDPB.ConnectionString", "select distinct(UserID),UserName,UserDisplayName from giftcharge").Tables[0];

            int max = allUser.Rows.Count < 30 ? 30 : allUser.Rows.Count;

            int has = allUser.Rows.Count;
            for (int i = 0; i < max; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = i;
                if (i < has)
                {
                    dr["UserID"] = allUser.Rows[i][0].ToString();
                    dr["UserName"] = allUser.Rows[i][1].ToString();
                    dr["UserDisplayName"] = allUser.Rows[i][2].ToString();


                    DataRow[] drs = alldt.Select("UserID='" + dr["UserID"] + "'");
                    string ids = string.Empty;
                    string names = string.Empty;
                    string dspnames = string.Empty;
                    foreach (DataRow drdept in drs)
                    {
                        ids += drdept["DepartmentID"].ToString() + ";";
                        names += drdept["DepartmentName"].ToString() + ";";
                        dspnames += drdept["DepartmentDisplayName"].ToString() + ";";
                    }
                    dr["DepartmentIDs"] = ids.Trim(new char[] { ';' }) ;
                    dr["DepartmentNames"] = names.Trim(new char[] { ';' });
                    dr["DepartmentDisplayNames"] = dspnames.Trim(new char[] { ';' });

                }
                dt.Rows.Add(dr);
            }

            rpList.DataSource = dt;
            rpList.DataBind();

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        DataAccess.Delete(typeof(Giftcharge), "1=1");
        string[] pairs = tb1.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string item in pairs)
        {
            string[] userdept = item.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            if (userdept.Length == 2)
            {
                User u = OrganizationHelper.GetUserInfo(new Guid(userdept[0]));
                foreach (string deptid in userdept[1].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    Department dept = OrganizationHelper.GetDepartmentInfo(new Guid(deptid));
                    Giftcharge gc = new Giftcharge();
                    gc.UserID = u.ID; gc.Username = u.Name;
                    gc.UserDisplayName = u.DisplayName;
                    gc.DepartmentID = dept.ID;
                    gc.DepartmentName = dept.Name;
                    gc.DepartmentDisplayName = dept.DisplayName;
                    gc.Add();


                }



            }
        }

        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>location=location;</script>");
    }
}
